package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;

import net.pws.common.security.spi.resource.IResource;
import net.pws.common.security.spi.resource.Resource;

import org.springframework.beans.BeanUtils;

public class ResourceDto extends Resource {
    
    public static ResourceDto from(IResource resource) {
        ResourceDto result = new ResourceDto();
        BeanUtils.copyProperties(resource, result, new String[] { "children",
                                                                 "parent" });
        IResource parent = resource.getParent();
        if (null != parent) {
            result.setParentId(parent.getId());
            result.setParentName(parent.getName());
        }
        return result;
    }
    
    public static List<ResourceDto> from(List<IResource> resources) {
        List<ResourceDto> result = new ArrayList<ResourceDto>();
        for (int i = 0; i < resources.size(); i++) {
            IResource resource = resources.get(i);
            result.add(ResourceDto.from(resource));
        }
        return result;
    }
    
    private String parentId;
    
    private String parentName;
    
    public String getParentId() {
        return parentId;
    }
    
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
    
    public String getParentName() {
        return parentName;
    }
    
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }
    
}
